package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.TrainPromoteShutnum;
import com.xmy.cultivate.entity.TrainPromoteShutnumDetail;
import com.xmy.cultivate.service.ITrainPromoteShutnumDetailService;
import com.xmy.cultivate.service.ITrainPromoteShutnumService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 教师晋升关数设置详情 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-10-18
 */
@RestController
@RequestMapping("/api/trainPromoteShutnumDetail")
public class TrainPromoteShutnumDetailController extends  BaseController {

    @Autowired
    ITrainPromoteShutnumService iTrainPromoteShutnumService;
    @Autowired
    ITrainPromoteShutnumDetailService iTrainPromoteShutnumDetailService;

    /**
     * 获取教师晋升关数设置详情列表
     *
     * @param pageNum
     * @param pageSize
     * @param lessonType
     * @param classTypeId
     * @param itemsId
     * @param versionId
     * @param passLevel
     * @param num
     * @return
     */
    @GetMapping("/trainPromoteShutnumDetailList")
    public Result trainPromoteShutnumDetailList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "lessonType", required = false) String lessonType,
                                                @RequestParam(value = "classTypeId", required = false) String classTypeId,
                                                @RequestParam(value = "itemsId", required = false) String itemsId,
                                                @RequestParam(value = "versionId", required = false) String versionId,
                                                @RequestParam(value = "passLevel", required = false) String passLevel,
                                                @RequestParam(value = "num", required = false) String num,
                                                @RequestParam(value = "shutnumId", required = false) String shutnumId
    ) {
        IPage<TrainPromoteShutnumDetail> iPage = new Page<>(pageNum, pageSize);
        iTrainPromoteShutnumDetailService.findAll(iPage, lessonType, classTypeId, itemsId, versionId, passLevel, num, shutnumId);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));
    }


    /**
     * 添加教师晋升关数设置详情
     *
     * @param
     * @return
     */
    @PostMapping("/addTrainPromoteShutnumDetail")
    public Result addTrainPromoteShutnumDetail(@RequestBody List<TrainPromoteShutnumDetail> trainPromoteShutnumDetailList) {
        for (TrainPromoteShutnumDetail trainPromoteShutnumDetail : trainPromoteShutnumDetailList) {
            QueryWrapper<TrainPromoteShutnumDetail> qw = new QueryWrapper<>();
            qw.eq("shutnum_id", trainPromoteShutnumDetail.getShutnumId());
            qw.eq("lesson_type", trainPromoteShutnumDetail.getLessonType());
            qw.eq("class_type_id", trainPromoteShutnumDetail.getClassTypeId());
            qw.eq("items_id", trainPromoteShutnumDetail.getItemsId());
            qw.eq("version_id", trainPromoteShutnumDetail.getVersionId());
            qw.eq("pass_level", trainPromoteShutnumDetail.getPassLevel());
            qw.eq("num", trainPromoteShutnumDetail.getNum());
            long count = iTrainPromoteShutnumDetailService.count(qw);
            if (count > 0) {
                return new Result(Code.SAVE_ERROR, null, "该数据已存在");
            }
            iTrainPromoteShutnumDetailService.save(trainPromoteShutnumDetail);
        }
        return new Result(Code.OK, null,"添加成功");
    }




/*
    @PostMapping("/addTrainPromoteShutnumDetail")
    public Result addTrainPromoteShutnumDetail(@RequestBody TrainPromoteShutnumDetail trainPromoteShutnumDetail) {
        QueryWrapper<TrainPromoteShutnumDetail> qw = new QueryWrapper<>();
        qw.eq("shutnum_id", trainPromoteShutnumDetail.getShutnumId());
        qw.eq("lesson_type", trainPromoteShutnumDetail.getLessonType());
        qw.eq("class_type_id", trainPromoteShutnumDetail.getClassTypeId());
        qw.eq("items_id", trainPromoteShutnumDetail.getItemsId());
        qw.eq("version_id", trainPromoteShutnumDetail.getVersionId());
        qw.eq("pass_level", trainPromoteShutnumDetail.getPassLevel());
        qw.eq("num", trainPromoteShutnumDetail.getNum());
        long count = iTrainPromoteShutnumDetailService.count(qw);
        if (count > 0) {
            return new Result(Code.SAVE_ERROR, null,"该数据已存在");
        }
        boolean save = iTrainPromoteShutnumDetailService.save(trainPromoteShutnumDetail);
        if (save) {
            return new Result(Code.OK, null,"添加成功");
        }
        return new Result(Code.ERROR, null,"添加失败");
    }
*/

    /**
     * 修改教师晋升关数设置详情
     *
     * @param trainPromoteShutnumDetail
     * @return
     */
    @PutMapping("/updateTrainPromoteShutnumDetail")
    public Result updateTrainPromoteShutnumDetail(@RequestBody TrainPromoteShutnumDetail trainPromoteShutnumDetail) {
        TrainPromoteShutnumDetail trainPromoteShutnumDetailOld = iTrainPromoteShutnumDetailService.getById(trainPromoteShutnumDetail.getId());
        if (Objects.isNull(trainPromoteShutnumDetailOld)) {
            return new Result(Code.SAVE_ERROR, null,"数据不存在,修改失败！");
        }

        QueryWrapper<TrainPromoteShutnumDetail> qw = new QueryWrapper<>();
        qw.ne("id", trainPromoteShutnumDetail.getId());
        qw.eq("shutnum_id", trainPromoteShutnumDetail.getShutnumId());
        qw.eq("lesson_type", trainPromoteShutnumDetail.getLessonType());
        qw.eq("class_type_id", trainPromoteShutnumDetail.getClassTypeId());
        qw.eq("items_id", trainPromoteShutnumDetail.getItemsId());
        qw.eq("version_id", trainPromoteShutnumDetail.getVersionId());
        qw.eq("pass_level", trainPromoteShutnumDetail.getPassLevel());
        qw.eq("num", trainPromoteShutnumDetail.getNum());
        long count = iTrainPromoteShutnumDetailService.count(qw);
        if (count > 0) {
            return new Result(Code.SAVE_ERROR, null,"该数据已存在");
        }
        boolean update = iTrainPromoteShutnumDetailService.updateById(trainPromoteShutnumDetail);
        if (update) {
            return new Result(Code.OK, null,"修改成功");
        }
        return new Result(Code.ERROR, null,"修改失败");
    }


    /**
     * 删除教师晋升关数设置详情
     * @param reMap
     * @return
     */
    @DeleteMapping("/deleteTrainPromoteShutnumDetail")
    public Result deleteTrainPromoteShutnumDetail(@RequestBody Map reMap) {
        List<String> ids = (List<String>) reMap.get("ids");
        if (ids == null || ids.isEmpty()) {
            return new Result(Code.DELETE_ERROR, "", "请选择要删除的数据");
        }
        QueryWrapper<TrainPromoteShutnumDetail> qw = new QueryWrapper<>();
        qw.in("id", ids);
        boolean b = iTrainPromoteShutnumDetailService.remove(qw);
        if (b) {
            return new Result(Code.DELETE_OK, "", Code.DELETE_OK_MSG);
        }
        return new Result(Code.ERROR, "", Code.DELETE_ERROR_MSG);
    }

}